package com.utils;


import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradePagePayModel;
import com.alipay.api.domain.AlipayTradeQueryModel;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.alipay.api.response.AlipayTradeQueryResponse;
// import com.wpl.bean.PayData;
import com.config.MyAliPayConfig;
import com.config.RequestAliPay;
import com.constans.AliPayCommonRequestConstants;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
@Slf4j
public class ZhifubaoUtil {


    //生成支付订单接口
    public static Map pay(RequestAliPay requestAliPay) throws AlipayApiException {


        MyAliPayConfig myAliPayConfig = MyAliPayConfig.builder()
                .serverUrl(AliPayCommonRequestConstants.SERVER_URL)  //支付官方地址
                .appId(AliPayCommonRequestConstants.APP_ID)
                .privateKey(AliPayCommonRequestConstants.PRIVATE_KEY)
                .alipayPublicKey(AliPayCommonRequestConstants.PUBLIC_KEY)
                .format(AliPayCommonRequestConstants.FORMAT)
                .charset(AliPayCommonRequestConstants.CHARSET)
                .signType(AliPayCommonRequestConstants.SIGN_TYPE).build();

        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl(myAliPayConfig.getServerUrl());
        alipayConfig.setAppId(myAliPayConfig.getAppId());
        alipayConfig.setPrivateKey(myAliPayConfig.getPrivateKey());
        alipayConfig.setAlipayPublicKey(myAliPayConfig.getAlipayPublicKey());
        alipayConfig.setFormat(myAliPayConfig.getFormat());
        alipayConfig.setCharset(myAliPayConfig.getCharset());
        alipayConfig.setSignType(myAliPayConfig.getSignType());

        //ali官方的类
        AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        AlipayTradePagePayModel model = new AlipayTradePagePayModel();

        //
        model.setOutTradeNo(requestAliPay.getOutTradeNo()+System.currentTimeMillis());
        model.setTotalAmount(String.valueOf(requestAliPay.getTotalAmount()));
        model.setSubject(requestAliPay.getSubject());
        model.setProductCode("FAST_INSTANT_TRADE_PAY");

        request.setBizModel(model);

        //回调
        request.setNotifyUrl(AliPayCommonRequestConstants.NOTIFY_URL);

        AlipayTradePagePayResponse response = alipayClient.pageExecute(request);
        log.info("== response={}",response.getBody());

        HashMap<Object, Object> map = new HashMap<>();
        if (response.isSuccess()) {
            System.out.println("调用成功");
            map.put("code",200);
            map.put("form",response.getBody());
        } else {
            map.put("code",400);
        }
        return map;
    }


    //查询接口
    public static Boolean queryResult(String tradeNo) throws AlipayApiException {
        MyAliPayConfig myAliPayConfig = MyAliPayConfig.builder()
                .serverUrl(AliPayCommonRequestConstants.SERVER_URL)  //支付官方地址
                .appId(AliPayCommonRequestConstants.APP_ID)
                .privateKey(AliPayCommonRequestConstants.PRIVATE_KEY)
                .alipayPublicKey(AliPayCommonRequestConstants.PUBLIC_KEY)
                .format(AliPayCommonRequestConstants.FORMAT)
                .charset(AliPayCommonRequestConstants.CHARSET)
                .signType(AliPayCommonRequestConstants.SIGN_TYPE).build();


        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl(myAliPayConfig.getServerUrl());
        alipayConfig.setAppId(myAliPayConfig.getAppId());
        alipayConfig.setPrivateKey(myAliPayConfig.getPrivateKey());
        alipayConfig.setAlipayPublicKey(myAliPayConfig.getAlipayPublicKey());
        alipayConfig.setFormat(myAliPayConfig.getFormat());
        alipayConfig.setCharset(myAliPayConfig.getCharset());
        alipayConfig.setSignType(myAliPayConfig.getSignType());

        AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        AlipayTradeQueryModel model = new AlipayTradeQueryModel();
        model.setOutTradeNo(tradeNo);
        request.setBizModel(model);
        AlipayTradeQueryResponse response = alipayClient.execute(request);
        System.out.println(response.getBody());
        return response.isSuccess();
    }
}
